package com.example.checkcrypt;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;


public class MainActivity extends Activity {

    private Context mCtxt;
    private String pDir = Environment.getExternalStorageDirectory() + "/com.foreveross.chameleon";
    ArrayList<String> data = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCtxt = this;
        data.add("a.html");
        data.add("a.txt");

        init();
        ListView listView = (ListView) findViewById(R.id.lv_crypts);
        listView.setAdapter(initListData());

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mCtxt, ViewActivity.class);
                intent.putExtra("path", pDir + "/" + data.get(position));
                System.out.println(pDir + "/" + data.get(position));
                startActivity(intent);
            }
        });
    }

    private void init() {
        Dialog dlg = new Builder(mCtxt).create();
        dlg.show();
        LayoutInflater inflater = LayoutInflater.from(mCtxt);
        View layer = inflater.inflate(R.layout.dialog_content, null);
        dlg.setContentView(layer);

        // copy file
        new AssetTool().copyAsset(mCtxt, pDir);
        ((TextView) findViewById(R.id.tv_dir)).setText(pDir);
        dlg.dismiss();
    }
    private ArrayAdapter<String> initListData() {

        ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,
                                                        data);
        return adapter;
    }
}
